package com.kun.admin.service.impl;

import com.kun.common.utils.JwtUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.admin.dao.AdminDao;
import com.kun.admin.entity.AdminEntity;
import com.kun.admin.service.AdminService;
import org.springframework.util.StringUtils;


@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AdminEntity> queryWrapper = new QueryWrapper<>();
        String memberName = (String) params.get("memberName");
        if (!StringUtils.isEmpty(memberName)){
            queryWrapper.like("member_name",memberName);
        }
        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public String login(AdminEntity admin) {
        QueryWrapper<AdminEntity> queryWrapper = new QueryWrapper<>();
        String memberName = admin.getMemberName();
        queryWrapper.eq("member_name",memberName);
        AdminEntity adminEntity = baseMapper.selectOne(queryWrapper);

        if (adminEntity==null){
            return null;
        }else {
            String memberPass = adminEntity.getMemberPass();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(admin.getMemberPass(), memberPass);
            if (matches){
                String jwtToken = JwtUtils.getJwtToken(adminEntity.getId().toString());
                return jwtToken;
            }else {
                return null;
            }
        }

    }

    @Override
    public boolean regist(AdminEntity admin) {
        QueryWrapper<AdminEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("member_name",admin.getMemberName());
        AdminEntity adminEntity = baseMapper.selectOne(queryWrapper);
        if(adminEntity!=null){
            return false;
        }else {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode(admin.getMemberPass());
            admin.setMemberPass(encode);
            baseMapper.insert(admin);
            return true;
        }
    }

    @Override
    public boolean updateAdmin(AdminEntity admin) {
        String memberName = admin.getMemberName();
        QueryWrapper<AdminEntity> queryWrapper = new QueryWrapper<>();
        AdminEntity entity = baseMapper.selectById(admin.getId());
        if(!entity.getMemberName().equals(admin.getMemberName())){
            queryWrapper.eq("member_name",memberName);
            AdminEntity adminEntity = baseMapper.selectOne(queryWrapper);
            if (adminEntity!=null){
                return false;
            }else {
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                String encode = passwordEncoder.encode(admin.getMemberPass());
                admin.setMemberPass(encode);
                baseMapper.updateById(admin);
                return true;
            }
        }else {
            baseMapper.updateById(admin);
            return true;
        }





    }

}